

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/avatar.gif">
  <link rel="icon" href="/img/avatar.gif">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="马振东">
  <meta name="keywords" content="Java,计算机,数据结构">
  
    <meta name="description" content="一、8种常见排序算法简介1. 分类图 2. 比较   名称 平均时间复杂度 最坏时间复杂度 稳定性 空间复杂度    插入排序 O(n2) O(n2) 稳定 O(1)   希尔排序 O(n1.3) O(nlogn)—O(n2) 不稳定 O(1)   选择排序 O(n2) O(n2) 不稳定 O(1)   堆排序 O(nlogn) O(nlogn) 不稳定 O(1)   冒泡排序 O(n2) O(n">
<meta property="og:type" content="article">
<meta property="og:title" content="Java八种常见算法">
<meta property="og:url" content="https://bolg.plumdong.top/2021/07/10/Java%E5%85%AB%E7%A7%8D%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/index.html">
<meta property="og:site_name" content="PlumDong Bolg">
<meta property="og:description" content="一、8种常见排序算法简介1. 分类图 2. 比较   名称 平均时间复杂度 最坏时间复杂度 稳定性 空间复杂度    插入排序 O(n2) O(n2) 稳定 O(1)   希尔排序 O(n1.3) O(nlogn)—O(n2) 不稳定 O(1)   选择排序 O(n2) O(n2) 不稳定 O(1)   堆排序 O(nlogn) O(nlogn) 不稳定 O(1)   冒泡排序 O(n2) O(n">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://bolg.plumdong.top/myimages/image-20201230195142206.png">
<meta property="og:image" content="https://bolg.plumdong.top/myimages/image-20210417160115290.png">
<meta property="og:image" content="https://bolg.plumdong.top/myimages/image-20210417160907171.png">
<meta property="og:image" content="https://bolg.plumdong.top/myimages/image-20210417161033163.png">
<meta property="og:image" content="https://bolg.plumdong.top/myimages/image-20210331084526757.png">
<meta property="article:published_time" content="2021-07-09T16:00:00.000Z">
<meta property="article:modified_time" content="2023-03-13T06:57:46.553Z">
<meta property="article:author" content="马振东">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://bolg.plumdong.top/myimages/image-20201230195142206.png">
  
  
  
  <title>Java八种常见算法 - PlumDong Bolg</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"bolg.plumdong.top","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="PlumDong Bolg" type="application/atom+xml">
</head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 60vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>PlumDong Bolg</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Java八种常见算法"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-07-10 00:00" pubdate>
          2021年7月10日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          5.9k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          50 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Java八种常见算法</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="一、8种常见排序算法简介"><a href="#一、8种常见排序算法简介" class="headerlink" title="一、8种常见排序算法简介"></a>一、8种常见排序算法简介</h1><h2 id="1-分类图"><a href="#1-分类图" class="headerlink" title="1. 分类图"></a>1. 分类图</h2><p><img src="/myimages/image-20201230195142206.png" srcset="/img/loading.gif" lazyload alt="image-20201230195142206"></p>
<h2 id="2-比较"><a href="#2-比较" class="headerlink" title="2. 比较"></a>2. 比较</h2><table>
<thead>
<tr>
<th>名称</th>
<th>平均时间复杂度</th>
<th>最坏时间复杂度</th>
<th>稳定性</th>
<th>空间复杂度</th>
</tr>
</thead>
<tbody><tr>
<td>插入排序</td>
<td>O(n2)</td>
<td>O(n2)</td>
<td>稳定</td>
<td>O(1)</td>
</tr>
<tr>
<td>希尔排序</td>
<td>O(n1.3)</td>
<td>O(nlogn)—O(n2)</td>
<td>不稳定</td>
<td>O(1)</td>
</tr>
<tr>
<td>选择排序</td>
<td>O(n2)</td>
<td>O(n2)</td>
<td>不稳定</td>
<td>O(1)</td>
</tr>
<tr>
<td>堆排序</td>
<td>O(nlogn)</td>
<td>O(nlogn)</td>
<td>不稳定</td>
<td>O(1)</td>
</tr>
<tr>
<td>冒泡排序</td>
<td>O(n2)</td>
<td>O(n2)</td>
<td>稳定</td>
<td>O(1)</td>
</tr>
<tr>
<td>快速排序</td>
<td>O(nlogn)</td>
<td>O(n2)</td>
<td>不稳定</td>
<td>O(logn)—O(n)</td>
</tr>
<tr>
<td>归并排序</td>
<td>O(nlogn)</td>
<td>O(nlogn)</td>
<td>稳定</td>
<td>O(n)</td>
</tr>
<tr>
<td>基数排序</td>
<td>O(d(n+r))</td>
<td>O(d(n+r))</td>
<td>稳定</td>
<td>O(n+r)</td>
</tr>
</tbody></table>
<h1 id="二、插入排序"><a href="#二、插入排序" class="headerlink" title="二、插入排序"></a>二、插入排序</h1><h2 id="1-思路"><a href="#1-思路" class="headerlink" title="1. 思路"></a>1. 思路</h2><p><strong>概念：</strong></p>
<p>插入 排序是一种简单直观且稳定的排序算法。</p>
<p><strong>原理：</strong></p>
<ol>
<li>把所有的元素分为两组，已排好序的和未排序的，开始时有序表只包含一个元素，无序表中包含n-1个元素。</li>
<li>排序过程中每次从未排序组中取出第一个元素，向已经排好序的组中插入。</li>
<li>倒序已经排序的元素，依次和待插入的元素进行比较，直到找到适当的位置。</li>
</ol>
<h2 id="2-代码实现"><a href="#2-代码实现" class="headerlink" title="2.代码实现"></a>2.代码实现</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insertSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> arr[])</span></span>&#123;<br>        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; arr.length; i++)&#123;<br>            <span class="hljs-keyword">int</span> rt = arr[i];<br>            <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> j = i - <span class="hljs-number">1</span>; j &gt;= <span class="hljs-number">0</span>; j--)&#123;<br>                <span class="hljs-keyword">if</span>(rt &lt; arr[j])&#123;<br>                    arr[j + <span class="hljs-number">1</span>] = arr[j];<br>                    arr[j] = rt;<br>                &#125;<span class="hljs-keyword">else</span>&#123;<br>                    <span class="hljs-keyword">break</span>;<br>                &#125; <br>            &#125;<br>        &#125;<br>    &#125;<br></code></pre></td></tr></table></figure>



<h1 id="三、希尔排序"><a href="#三、希尔排序" class="headerlink" title="三、希尔排序"></a>三、希尔排序</h1><p>希尔排序也是一种插入排序，他是简单插入排序经过改进之后的一个更高效的版本，也称为缩小增量排序。</p>
<p>插入排序的平均时间复杂度也是 **O(n^2)**，空间复杂度为常数阶 **O(1)**，具体时间复杂度和数组的有序性也是有关联的。</p>
<h2 id="1-思路图解"><a href="#1-思路图解" class="headerlink" title="1. 思路图解"></a>1. 思路图解</h2><p><strong>基本思想：</strong> 希尔排序是把记录按下标的一定增量分组，对每组使用直接插入排序算法排序。随着增量逐渐减少，每组包含的关键词越来越多，当增量减至1时，整个文件恰被分成一组，算法终止。</p>
<p><strong>解题过程</strong>：</p>
<p>（1）第一趟排序：初始增量 为：<strong>length/2 = 4</strong>，把8个数分成4组，每组单独排序</p>
<p><img src="/myimages/image-20210417160115290.png" srcset="/img/loading.gif" lazyload alt="初始增量第一趟"></p>
<p>（2）第二趟，增量缩小为 2</p>
<p><img src="/myimages/image-20210417160907171.png" srcset="/img/loading.gif" lazyload alt="image-20210417160907171"></p>
<p>（3）第三趟，增量缩小为 1,得到最终排序结果</p>
<p><img src="/myimages/image-20210417161033163.png" srcset="/img/loading.gif" lazyload alt="image-20210417161033163"></p>
<h2 id="2-代码实现-1"><a href="#2-代码实现-1" class="headerlink" title="2.代码实现"></a>2.代码实现</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">sort</span><span class="hljs-params">(Comparable[] arr)</span> </span>&#123;<br>    <span class="hljs-keyword">int</span> j;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> gap = arr.length / <span class="hljs-number">2</span>; gap &gt;  <span class="hljs-number">0</span>; gap /= <span class="hljs-number">2</span>) &#123;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = gap; i &lt; arr.length; i++) &#123;<br>            Comparable tmp = arr[i];<br>            <span class="hljs-keyword">for</span> (j = i; j &gt;= gap &amp;&amp; tmp.compareTo(arr[j - gap]) &lt; <span class="hljs-number">0</span>; j -= gap) &#123;<br>                arr[j] = arr[j - gap];<br>            &#125;<br>            arr[j] = tmp;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="四、选择排序"><a href="#四、选择排序" class="headerlink" title="四、选择排序"></a>四、选择排序</h1><h2 id="1-思路图解-1"><a href="#1-思路图解-1" class="headerlink" title="1. 思路图解"></a>1. 思路图解</h2><p><strong>基本思想：</strong>选择排序是一种简单直观的排序算法，无论什么数据进去都是 O(n²) 的时间复杂度。所以用到它的时候，数据规模越小越好。唯一的好处就是不占用额外的内存空间。</p>
<p><strong>算法步骤：</strong></p>
<p>（1）首先在未排序序列中找到最小（大）元素，存放到排序序列的起始位置。</p>
<p>（2）再从剩余未排序元素中继续寻找最小（大）元素，然后放到已排序序列的末尾。</p>
<p>（3）重复第（2）步，直到所有元素均排序完毕。</p>
<h2 id="2-代码实现-2"><a href="#2-代码实现-2" class="headerlink" title="2.代码实现"></a>2.代码实现</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">selectSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] arr)</span></span>&#123;<br>    <span class="hljs-keyword">int</span> min;<br>    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;i&lt;arr.length;i++)&#123;<br>        min = i;<br>        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> j = i;j&lt;arr.length;j++)&#123;<br>            <span class="hljs-keyword">if</span>(arr[j] &lt; arr[min])&#123;<br>                min = j;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">if</span>(min != i) &#123;<br>            <span class="hljs-keyword">int</span> temp = arr[i];<br>            arr[i] = arr[min];<br>            arr[min] = temp;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="五、堆排序"><a href="#五、堆排序" class="headerlink" title="五、堆排序"></a>五、堆排序</h1><h2 id="1-思路图解-2"><a href="#1-思路图解-2" class="headerlink" title="1. 思路图解"></a>1. 思路图解</h2><p><strong>基本思想：</strong></p>
<p>堆积是一个近似完全二叉树的结构，并同时满足堆积的性质：即子结点的键值或索引总是小于（或者大于）它的父节点。堆排序可以说是一种利用堆的概念来排序的选择排序。分为两种方法：</p>
<p>（1）大顶堆：每个节点的值都大于或等于其子节点的值，在堆排序算法中用于升序排列；</p>
<p>（2）小顶堆：每个节点的值都小于或等于其子节点的值，在堆排序算法中用于降序排列；</p>
<p><strong>算法步骤：</strong></p>
<p>（1）创建一个堆 H[0……n-1]；</p>
<p>（2）把堆首（最大值）和堆尾互换；</p>
<p>（3）把堆的尺寸缩小 1，并调用 shift_down(0)，目的是把新的数组顶端数据调整到相应位置；</p>
<p>（4）重复步骤 2，直到堆的尺寸为 1。</p>
<h2 id="2-代码实现-3"><a href="#2-代码实现-3" class="headerlink" title="2.代码实现"></a>2.代码实现</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">heapSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums)</span> </span>&#123;<br>       <span class="hljs-keyword">int</span> size = nums.length;<br>       <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = size/<span class="hljs-number">2</span>-<span class="hljs-number">1</span>; i &gt;=<span class="hljs-number">0</span>; i--) &#123;<br>           adjust(nums, size, i);<br>       &#125;<br>       <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = size - <span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">1</span>; i--) &#123;<br>           <span class="hljs-keyword">int</span> temp = nums[<span class="hljs-number">0</span>];<br>           nums[<span class="hljs-number">0</span>] = nums[i];<br>           nums[i] = temp;<br>           adjust(nums, i, <span class="hljs-number">0</span>);<br>       &#125;<br>   &#125;<br>   <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">adjust</span><span class="hljs-params">(<span class="hljs-keyword">int</span> []nums, <span class="hljs-keyword">int</span> len, <span class="hljs-keyword">int</span> index)</span> </span>&#123;<br>       <span class="hljs-keyword">int</span> l = <span class="hljs-number">2</span> * index + <span class="hljs-number">1</span>;<br>       <span class="hljs-keyword">int</span> r = <span class="hljs-number">2</span> * index + <span class="hljs-number">2</span>;<br>       <span class="hljs-keyword">int</span> maxIndex = index;<br>       <span class="hljs-keyword">if</span> (l&lt;len&amp;&amp;nums[l]&gt;nums[maxIndex])maxIndex = l;<br>       <span class="hljs-keyword">if</span> (r&lt;len&amp;&amp;nums[r]&gt;nums[maxIndex])maxIndex = r;<br>       <span class="hljs-keyword">if</span> (maxIndex != index) &#123;<br>           <span class="hljs-keyword">int</span> temp = nums[maxIndex];<br>           nums[maxIndex] = nums[index];<br>           nums[index] = temp;<br>           adjust(nums, len, maxIndex);<br>       &#125;<br>   &#125;<br></code></pre></td></tr></table></figure>



<h1 id="六、冒泡排序"><a href="#六、冒泡排序" class="headerlink" title="六、冒泡排序"></a>六、冒泡排序</h1><h2 id="1-思路图解-3"><a href="#1-思路图解-3" class="headerlink" title="1. 思路图解"></a>1. 思路图解</h2><p><strong>基本思想：</strong>两个数比较大小，较大的数下沉，较小的数冒起来。</p>
<p><strong>算法步骤：</strong></p>
<p>（1）比较相邻的两个数据，如果第二个数小，就交换位置。</p>
<p>（2）从后向前两两比较，一直到比较最前两个数据。最终最小数被交换到起始的位置，这样第一个最小数的位置就排好了。</p>
<p>（3）继续重复上述过程，依次将第2.3…n-1个最小数排好位置。</p>
<p><img src="/myimages/image-20210331084526757.png" srcset="/img/loading.gif" lazyload alt="image-20210331084526757"></p>
<h2 id="2-代码实现-4"><a href="#2-代码实现-4" class="headerlink" title="2.代码实现"></a>2.代码实现</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">BubbleSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] arr)</span></span>&#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i&lt;arr.length-<span class="hljs-number">1</span>;i++)&#123;<br>        <span class="hljs-keyword">boolean</span> flag = <span class="hljs-keyword">false</span>;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j=<span class="hljs-number">0</span>;j&lt;arr.length-<span class="hljs-number">1</span>-i;j++)&#123;<br>            <span class="hljs-keyword">if</span> (arr[j]&gt;arr[j+<span class="hljs-number">1</span>])&#123;<br>                <span class="hljs-keyword">int</span> t = arr[j];<br>                arr[j] = arr[j+<span class="hljs-number">1</span>];<br>                arr[j+<span class="hljs-number">1</span>] = t;<br>                flag = <span class="hljs-keyword">true</span>;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (flag == <span class="hljs-keyword">false</span>)<br>            <span class="hljs-keyword">break</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="3-总结"><a href="#3-总结" class="headerlink" title="3.总结"></a>3.总结</h2><p>数据的顺序排好之后，冒泡算法仍然会继续进行下一轮的比较，直到arr.length-1次，后面的比较没有意义的。</p>
<p>因此设置标志位flag，如果发生了交换flag设置为true；如果没有交换就设置为false。</p>
<p>这样当一轮比较结束后如果flag仍为false，即：这一轮没有发生交换，说明数据的顺序已经排好，没有必要继续进行下去。</p>
<h1 id="七、快速排序"><a href="#七、快速排序" class="headerlink" title="七、快速排序"></a>七、快速排序</h1><h2 id="1-思路图解-4"><a href="#1-思路图解-4" class="headerlink" title="1. 思路图解"></a>1. 思路图解</h2><p><strong>基本思想：（分治）</strong></p>
<p>在平均状况下，排序 n 个项目要 Ο(nlogn) 次比较。在最坏状况下则需要 Ο(n2) 次比较，但这种状况并不常见。事实上，快速排序通常明显比其他 Ο(nlogn) 算法更快，因为它的内部循环（inner loop）可以在大部分的架构上很有效率地被实现出来。</p>
<p><strong>算法步骤：</strong></p>
<p>（1）先从数列中取出一个数作为key值；</p>
<p>（2）将比这个数小的数全部放在它的左边，大于或等于它的数全部放在它的右边；</p>
<p>（3）对左右两个小数列重复第二步，直至各区间只有1个数。</p>
<h2 id="2-代码实现-5"><a href="#2-代码实现-5" class="headerlink" title="2.代码实现"></a>2.代码实现</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-comment">//快排</span><br>    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">QuickSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums,<span class="hljs-keyword">int</span> low,<span class="hljs-keyword">int</span> high)</span></span>&#123;<br><br>        <span class="hljs-keyword">if</span>(low&lt;high) &#123;<br>            <span class="hljs-keyword">int</span> pivotpos = partition(nums,low,high);<br>            QuickSort(nums,low,pivotpos-<span class="hljs-number">1</span>);<br>            QuickSort(nums,pivotpos+<span class="hljs-number">1</span>,high);<br>        &#125;<br>    &#125;<br>    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">partition</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums,<span class="hljs-keyword">int</span> low,<span class="hljs-keyword">int</span> high)</span></span>&#123;<br>        <span class="hljs-keyword">int</span> pivot = nums[low];<br>        <span class="hljs-keyword">while</span>(low&lt;high) &#123;<br>            <span class="hljs-keyword">while</span>(low&lt;high &amp;&amp; nums[high]&gt;=pivot)--high;<br>            nums[low] = nums[high];<br>            <span class="hljs-keyword">while</span>(low&lt;high &amp;&amp; nums[low]&lt;=pivot) ++low;<br>            nums[high] = nums[low];<br>        &#125;<br>        nums[low] = pivot;<br>        <span class="hljs-keyword">return</span> low;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        <span class="hljs-keyword">int</span>[] a = &#123;<span class="hljs-number">9</span>,<span class="hljs-number">5</span>,<span class="hljs-number">1</span>,<span class="hljs-number">7</span>,<span class="hljs-number">5</span>,<span class="hljs-number">63</span>,<span class="hljs-number">4</span>,<span class="hljs-number">7</span>,<span class="hljs-number">6</span>,<span class="hljs-number">1</span>,<span class="hljs-number">77</span>&#125;;<br>        Solution solution = <span class="hljs-keyword">new</span> Solution();<br>        solution.QuickSort(a,<span class="hljs-number">0</span>,a.length-<span class="hljs-number">1</span>);<br>        System.out.println(Arrays.toString(a));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h1 id="八、归并排序"><a href="#八、归并排序" class="headerlink" title="八、归并排序"></a>八、归并排序</h1><h2 id="1-思路-1"><a href="#1-思路-1" class="headerlink" title="1. 思路"></a>1. 思路</h2><p><strong>基本思想：</strong>归并排序同样是采用分治法的一个非常典型的应用。作为一种典型的分而治之思想的算法应用，归并排序的实现由两种方法：</p>
<p>（1）自上而下的递归（所有递归的方法都可以用迭代重写，所以就有了第 2 种方法）；</p>
<p>（2）自下而上的迭代；</p>
<p><strong>算法步骤：</strong></p>
<p>（1）申请空间，使其大小为两个已经排序序列之和，该空间用来存放合并后的序列；</p>
<p>（2）设定两个指针，最初位置分别为两个已经排序序列的起始位置；</p>
<p>（3）比较两个指针所指向的元素，选择相对小的元素放入到合并空间，并移动指针到下一位置；</p>
<p>（4）重复步骤 3 直到某一指针达到序列尾；</p>
<p>（5）将另一序列剩下的所有元素直接复制到合并序列尾。</p>
<h2 id="2-代码实现-6"><a href="#2-代码实现-6" class="headerlink" title="2.代码实现"></a>2.代码实现</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs java"> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">mergeSortInOrder</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] arr,<span class="hljs-keyword">int</span> bgn,<span class="hljs-keyword">int</span> mid, <span class="hljs-keyword">int</span> end)</span></span>&#123;<br>    <span class="hljs-keyword">int</span> l = bgn, m = mid +<span class="hljs-number">1</span>, e = end;<br>    <span class="hljs-keyword">int</span>[] arrs = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[end - bgn + <span class="hljs-number">1</span>];<br>    <span class="hljs-keyword">int</span> k = <span class="hljs-number">0</span>;<br>    <span class="hljs-keyword">while</span>(l &lt;= mid &amp;&amp; m &lt;= e)&#123;<br>        <span class="hljs-keyword">if</span>(arr[l] &lt; arr[m])&#123;<br>            arrs[k++] = arr[l++];<br>        &#125;<span class="hljs-keyword">else</span>&#123;<br>            arrs[k++] = arr[m++];<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">while</span>(l &lt;= mid)&#123;<br>        arrs[k++] = arr[l++];<br>    &#125;<br>    <span class="hljs-keyword">while</span>(m &lt;= e)&#123;<br>        arrs[k++] = arr[m++];<br>    &#125;<br>    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; arrs.length; i++)&#123;<br>        arr[i + bgn] = arrs[i];<br>    &#125;<br>&#125;<br> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">mergeSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] arr, <span class="hljs-keyword">int</span> bgn, <span class="hljs-keyword">int</span> end)</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-keyword">if</span>(bgn &gt;= end)&#123;<br>        <span class="hljs-keyword">return</span>;<br>    &#125;<br>    <span class="hljs-keyword">int</span> mid = (bgn + end) &gt;&gt; <span class="hljs-number">1</span>;<br>    mergeSort(arr,bgn,mid);<br>    mergeSort(arr,mid + <span class="hljs-number">1</span>, end);<br>    mergeSortInOrder(arr,bgn,mid,end);<br>&#125;<br></code></pre></td></tr></table></figure>



<h1 id="九、基数排序"><a href="#九、基数排序" class="headerlink" title="九、基数排序"></a>九、基数排序</h1><h2 id="1-思路-2"><a href="#1-思路-2" class="headerlink" title="1. 思路"></a>1. 思路</h2><p><strong>基本思想：</strong>基数排序是一种非比较型整数排序算法，其原理是将整数按位数切割成不同的数字，然后按每个位数分别比较。由于整数也可以表达字符串（比如名字或日期）和特定格式的浮点数，所以基数排序也不是只能使用于整数。</p>
<p><strong>算法步骤：</strong></p>
<p>（1）将所有待比较数值（正整数）统一为同样的数位长度，数位较短的数前面补零。<br>（2） 从最低位开始，依次进行一次排序。<br>（3） 这样从最低位排序一直到最高位排序完成以后, 数列就变成一个有序序列。</p>
<h2 id="2-代码实现-7"><a href="#2-代码实现-7" class="headerlink" title="2.代码实现"></a>2.代码实现</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">countSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] arr)</span></span>&#123;<br>        <span class="hljs-keyword">int</span>[] bk = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-number">19</span>];<br>        Integer max = Integer.MIN_VALUE;<br>        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++)&#123;<br>            <span class="hljs-keyword">if</span>(max &lt; Math.abs(arr[i])) max = arr[i];<br>        &#125;<br>        <span class="hljs-keyword">if</span>(max &lt; <span class="hljs-number">0</span>) max = -max;<br>        max = max.toString().length();<br>        <span class="hljs-keyword">int</span> [][] bd = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-number">19</span>][arr.length];<br>        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> k = <span class="hljs-number">0</span>; k &lt; max; k++) &#123;<br>            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) &#123;<br>                <span class="hljs-keyword">int</span> value = (<span class="hljs-keyword">int</span>)(arr[i] / (Math.pow(<span class="hljs-number">10</span>,k)) % <span class="hljs-number">10</span>);<br>                bd[value+<span class="hljs-number">9</span>][bk[value+<span class="hljs-number">9</span>]++] = arr[i];<br>            &#125;<br>            <span class="hljs-keyword">int</span> fl = <span class="hljs-number">0</span>;<br>            <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> l = <span class="hljs-number">0</span>; l &lt; <span class="hljs-number">19</span>; l++)&#123;<br>                <span class="hljs-keyword">if</span>(bk[l] != <span class="hljs-number">0</span>)&#123;<br>                    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> s = <span class="hljs-number">0</span>; s &lt; bk[l]; s++)&#123;<br>                        arr[fl++] = bd[l][s];<br>                    &#125;<br>                &#125;<br>            &#125;<br>            bk = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-number">19</span>];<br>            fl = <span class="hljs-number">0</span>;<br>        &#125;<br>    &#125;<br></code></pre></td></tr></table></figure>




                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/Java%E5%9F%BA%E7%A1%80/" class="category-chain-item">Java基础</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Java/">#Java</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Java八种常见算法</div>
      <div>https://bolg.plumdong.top/2021/07/10/Java八种排序算法/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>马振东</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年7月10日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/08/20/%E5%AD%90%E4%B8%B2%E5%8C%B9%E9%85%8D%E2%80%94KMP%E7%AE%97%E6%B3%95/" title="子串匹配—KMP算法实现">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">子串匹配—KMP算法实现</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/07/06/Java%E4%B8%AD%E5%AE%9E%E7%8E%B0a==1&amp;&amp;a==2&amp;&amp;a==3/" title="Java中实现a==1&amp;&amp;a==2&amp;&amp;a==3">
                        <span class="hidden-mobile">Java中实现a==1&amp;&amp;a==2&amp;&amp;a==3</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
